package com.nuc.wxh.vaccine_appointment.vaccines.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc.wxh.vaccine_appointment.base.result.R;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AdminDoctor;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.SysUser;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.SysVaccinator;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.AdminDoctorVo;
import com.nuc.wxh.vaccine_appointment.vaccines.service.SysUserService;
import com.nuc.wxh.vaccine_appointment.vaccines.service.SysVaccinatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 接种人信息 前端控制器
 * </p>
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@Api(tags = "接种者管理")
@CrossOrigin
@RestController
@RequestMapping("/user/sys-vaccinator")
public class SysVaccinatorController {
    @Autowired
    private SysVaccinatorService sysVaccinatorService;


    @ApiOperation("查询接种人列表")
    @GetMapping("list/{id}")
    public R list(@ApiParam(value = "查询对象条件", required = true) @PathVariable String id) {

        Map<String, Object> map = new HashMap<>();
        map.put("sys_user_id", id);
        List<SysVaccinator> list = sysVaccinatorService.listByMap(map);
        return R.ok().data("rows", list);
    }

    @ApiOperation("新增接种者")
    @PostMapping("save")
    public R save(@ApiParam(value = "用户对象", required = true) @RequestBody SysVaccinator sysVaccinator) {
        QueryWrapper<SysVaccinator> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(sysVaccinator.getIdCard())) {
            wrapper.likeRight("id_card", sysVaccinator.getIdCard()).likeRight("sys_user_id", sysVaccinator.getSysUserId());
        }
        if (sysVaccinatorService.count(wrapper) > 0) {
            return R.error().message("已有重复接种者信息");
        }
        boolean result = sysVaccinatorService.save(sysVaccinator);
        if (result) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @ApiOperation(value = "根据ID删除接种者", notes = "逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "接种者id", required = true) @PathVariable String id) {
        boolean result = sysVaccinatorService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("接种者修改")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "接种者对象", required = true) @RequestBody SysVaccinator sysVaccinator) {

        boolean result = sysVaccinatorService.updateById(sysVaccinator);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据id获取接种者信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "接种者ID", required = true) @PathVariable String id) {
        SysVaccinator sysVaccinator = sysVaccinatorService.getById(id);
        if (sysVaccinator != null) {
            return R.ok().data("item", sysVaccinator);
        } else {
            return R.error().message("数据不存在");
        }
    }


}

